﻿<!--#INCLUDE FILE="config.asp"-->

<%
action = request("Code")

	If (Request("code") <> "") Then
	
		code = Request("code")
		
		sqlstring = "SELECT dbo.Codes.*,dbo.Products.ProductName,dbo.Propertys.PropertyName  "
		sqlstring = sqlstring & " FROM dbo.Codes "
		sqlstring = sqlstring & " INNER JOIN dbo.Products ON dbo.Products.ProductID = dbo.Codes.ProductID "
		sqlstring = sqlstring & " INNER JOIN dbo.Propertys ON dbo.Propertys.PropertyID = dbo.Codes.PropertyID "
		sqlstring = sqlstring & " WHERE Code = '"& code & "'"
		
		
		set objConnect = server.createObject("ADODB.Connection")

		objConnect.Open conStr

		set TblTemp = server.createobject("Adodb.Recordset")
				
			TblTemp.Open Sqlstring, objConnect
			
			' check code has existing?
			if (TblTemp.EOF = TRUE) then
				output = "0"
			end if
			
			' if existing then...
			Do Until TblTemp.EOF
				
				ExDate = TblTemp("ExpDate")
				multiUses = TblTemp("MultiUse")
				codeId = TblTemp("CodeID")
				DayExtends = TblTemp("DaysExtended")
				Nights =  TblTemp("Nights")
				
				' if existing and multiuse = 1 and expired then output = 1 --> extended day
				if (multiUses = 1 and Date > DateAdd("d",DayExtends,ExDate)) Then 	
					output = "1"				
					Exit Do
				End If
				
				' if existing and multiuse = 0 then check code is used by any travel?
				if (multiUses = 0) Then 
				
					sqlstring1 = "select TOP 1 * from travels where codeID = " & codeId
					
					set objConnect1 = server.createObject("ADODB.Connection")

					objConnect1.Open conStr

					set TblTemp1 = server.createobject("Adodb.Recordset")
					
					TblTemp1.Open sqlstring1, objConnect1
					' if is not used by any travel then check expired ..
					if (TblTemp1.EOF = TRUE) then
						if (Date > DateAdd("d",DayExtends,ExDate)) Then 	
							output = "1"				
							Exit Do
						End If
					else 
						statusId = TblTemp1("StatusID")
						If (statusId = 3) then
							if (Date > DateAdd("d",DayExtends,ExDate)) Then 	
								output = "1"				
								Exit Do
							End If
						else
							output = "2" ' used by any travel
							Exit Do
						end if
					end if
				End If
				
				codeID = TblTemp("CodeID")
				Origina = TblTemp("Originator")
				Camp = TblTemp("Campaign")
				ProductName = TblTemp("ProductName")
				PropertyName = TblTemp("PropertyName")		
				OfferDetail = TblTemp("OfferDetails")
				dim x
				x = replace(OfferDetail,"/","<br/>")
				Cost = TblTemp("Cost")
				TransportCost = TblTemp("TransportationCost")
				ExpDate = DateAdd("d",DayExtends,ExDate)
				Nights = TblTemp("Nights")
				
				output = "{"
				output = output + """codeID"":" & """"&codeID&""","
				output = output + """Origina"":" & """"&Origina&""","
				output = output + """Camp"":" & """"&Camp&""","
				output = output + """ProductName"":" & """"&ProductName&""","
				output = output + """Night"":" & """"&Nights&""","
				output = output + """OfferDetail"":" & """"&x&""","
				output = output + """Cost"":" & """"&Cost&""","
				output = output + """Nights"":" & """"&Nights&""","
				output = output + """TransCost"":" & """"&TransportCost&""","
				output = output + """ExpiredDate"":" & """"&FormatDateTime(ExpDate,2)&""","
				output = output + """PropertyName"":" & """"&PropertyName&""""
				output = output + "}"
					
				
				TblTemp.MoveNext
			Loop
	End If

	response.write(output)

%>